﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zyui
{
    public partial class BaseSearch : UserControl
    {
        public int _kjm;
        public List<string> DbKayList;
        public List<string> DbTitleList;
        public List<string> SearchZdList;
        public List<string> FiledsList;
        public event btnclick BtnScarchClick;
        private string _DataType;
        public string Sql = string.Empty;
        public int smklx = 0;
        public string ystime = string.Empty, yetime = string.Empty;
        public BaseSearch()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 获取书目库
        /// </summary>
        /// <param name="scdm">文献类型(图书，期刊，非书资料)</param>
        public void AddDbList(string scdm)
        {
            this.skKay.Items.Clear();
            this.DbKayList = new List<string>();
            this.DbKayList.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "select * from 书目数据库定义 where  文献类型='" + scdm + "' order by 库键码";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        this.skKay.Items.Add(dRow["书目库名"].ToString().Trim());
                        this.DbKayList.Add(dRow["库键码"].ToString());
                    }
                }
                if (this.skKay.Items.Count > 0)
                {
                    this.skKay.SelectedIndex = 0;
                    this._kjm = int.Parse(this.DbKayList[0].ToString());
                }
            }
        }
        private void skKay_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.skKay.SelectedIndex;
            this._kjm = int.Parse(this.DbKayList[selectedIndex].ToString());
            this.jsxKay.Items.Clear();
            this.DbTitleList = new List<string>();
            this.SearchZdList = new List<string>();
            this.FiledsList = new List<string>();
            this.FiledsList.Clear();
            this.SearchZdList.Clear();
            this.DbTitleList.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                string sql= "select * from 检索途径定义 where 库键码=" + this._kjm ;
                if (smklx == 0)
                    _open.SqlCmd = sql + " and 馆藏维护浏览顺序 is not null order by 馆藏维护浏览顺序";
                if (smklx == 1)
                    _open.SqlCmd  = sql + " and 采购浏览顺序 is not null order by 采购浏览顺序";
                if (smklx == 2)
                    _open.SqlCmd = sql + " and 编目浏览顺序 is not null order by 编目浏览顺序";
                if (smklx == 3)
                    _open.SqlCmd = sql + " and 典藏分配浏览顺序 is not null order by 典藏分配浏览顺序";
                //Console.WriteLine(_open.SqlCmd);
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        string tname = dRow["所属表"].ToString().Trim();
                        string zdm = dRow["字段名称"].ToString().Trim();
                        this.jsxKay.Items.Add(dRow["字段名称"].ToString().Trim());
                        this.DbTitleList.Add(dRow["所属表"].ToString());
                        this.SearchZdList.Add(dRow["字段名称"].ToString().Trim());
                        if (tname.Equals("馆藏书目库"))
                        {
                            try
                            {
                                bool xs = Convert.ToBoolean(dRow["是否显示"].ToString().Trim());
                                if (xs)
                                {
                                    this.FiledsList.Add(zdm);
                                }
                            }
                            catch { }
                        }
                    }
                }
                if (this.jsxKay.Items.Count > 0)
                {
                    this.jsxKay.SelectedIndex = 0;
                }
            }
        }
        private void jsxKay_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.jsxKay.SelectedIndex;
            string tableName = this.DbTitleList[index].ToString().Trim();
            string fildName = this.SearchZdList[index].ToString().Trim();
            if (fildName.Trim().Equals("登录号") || fildName.Trim().Equals("条形码"))
                this.acCheck.Enabled = true;
            else
                this.acCheck.Enabled = false;
            this.jsfsKay.Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "所属表='" + tableName + "' and 字段名称='" + fildName + "'";
                _open.Reset();
                _open.ConditionExpress = sql;
                string zdlx = _open.GetField("检索途径定义", "字段类型", false).ToString();
                if (zdlx.ToLower().Equals("c"))
                {
                    if (fildName.Trim().Equals("登录号") || fildName.Trim().Equals("条形码"))
                        this.jsfsKay.Items.Add("介于");
                    this.jsfsKay.Items.Add("前方一致");
                    this.jsfsKay.Items.Add("中间一致");
                    this.jsfsKay.Items.Add("后方一致");
                    this.jsfsKay.Items.Add("绝对一致");
                }
                else
                {
                    this.jsfsKay.Items.Add("等于");
                    this.jsfsKay.Items.Add("大于");
                    this.jsfsKay.Items.Add("小于");
                    this.jsfsKay.Items.Add("介于");
                }
                this.jsfsKay.SelectedIndex = 0;
            }
        }
        public void AddConfig(int smk)
        {
            this.smklx = smk;
            DateTime year = DateTime.Now.AddDays(-180);
            DateChoser.AddTo(this.sTime, year);
            DateChoser.AddTo(this.eTime);
            DateChoser.AddTo(this.dsTime, year);
            DateChoser.AddTo(this.deTime);
            DateTime year1 = DateTime.Now.AddDays(-15);
            DateChoser.AddTo(this.ysTime, year1);
            DateChoser.AddTo(this.yeTime);
            this.pzdKay.SelectedIndex = 0;
            this.pfsKay.SelectedIndex = 0;
            this.ztKay.Items.Clear();
            if (smklx == 0)
            {
                this.ztKay.Items.Add("不限");
                this.ztKay.Items.Add("入藏");
                this.ztKay.Items.Add("验收");
                this.ztKay.Items.Add("采购");
                this.ztKay.Items.Add("其他");
            }
            if (smklx == 1)
            {
                this.ztKay.Items.Add("采购");
                this.ztKay.Items.Add("验收");
                this.ztKay.Items.Add("入藏");
                this.ztKay.Items.Add("不限");
            }
            if (smklx == 2)
            {
                this.ztKay.Items.Add("采购");
                this.ztKay.Items.Add("验收");
                this.ztKay.Items.Add("不限");
            }
            this.ztKay.SelectedIndex = 0;
            this.AddDbList("图书");
        }
        public delegate void btnclick(object sender, EventArgs e);

        private void stxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.sBtn_Click(sender, e);
            }
        }

        private void sBtn_Click(object sender, EventArgs e)
        {
            int index = this.jsxKay.SelectedIndex;
            string tableName = this.DbTitleList[index].ToString().Trim();
            string fildName = this.SearchZdList[index].ToString().Trim();
            using (DbOperHandler _open = new Common().Doh())
            {
                this._DataType = _open.dbType.ToString();
                //Console.WriteLine("数据库类型:"+ _open.odbType.ToString());
                _open.Reset();
                _open.ConditionExpress = "所属表='" + tableName + "' and 字段名称='" + fildName + "'";
                string jslx = _open.GetField("检索途径定义", "类型", false).ToString();
                string zdlx = _open.GetField("检索途径定义", "字段类型", false).ToString();
                string type = this.jsfsKay.Text;
                string languageData = SqlText.BuindPosSql(this.sTxt.Text.ToString().ToUpper()).Replace("?", "_");
                languageData = languageData.Replace("*", "%");
                if (tableName != "馆藏书目库")
                {
                    languageData = SqlText.GetLanguage(languageData);
                }
                int jgs = 100;
                try
                {
                    jgs = int.Parse(this.xzTxt.Text.Trim()); ;
                }
                catch { }
                string fileds = "";
                if (this.FiledsList.Count > 0)
                {
                    foreach (string str in this.FiledsList)
                    {
                        fileds += "g." + str + ",";
                    }
                    if (!string.IsNullOrEmpty(fileds))
                        fileds += "g.主键码 ";
                    string sql = SqlText.BuindSql(tableName, fildName, languageData, type, Convert.ToInt32(jslx), this._DataType, this.nullCheck.Checked, this.fsCheck.Checked);
                    if (this._DataType == DatabaseType.MYSQL.ToString())
                    {
                        sql = "SELECT  "+fileds+" From 馆藏书目库 g WHERE " + sql;
                    }
                    else if (this._DataType == DatabaseType.ORACLE.ToString())
                    {
                        sql = "SELECT g.标准编码,g.索书号, g.题名2 as 题名,g.责任者2 AS 责任者,g.出版者2 AS 出版者, g.出版日期, g.版次,g.主键码  From 馆藏书目库 g WHERE ROWNUM<" + jgs + " AND " + sql;
                    }
                    else if (_open.odbType.ToString().Equals("ORACLE"))
                    {
                        sql = "SELECT g.标准编码, g.索书号,g.题名2 as 题名,g.责任者2 AS 责任者,g.出版者2 AS 出版者, g.出版日期, g.版次,g.主键码  From  馆藏书目库 g WHERE ROWNUM<" + jgs + " AND " + sql;
                        //sql = "SELECT 主键码, DUMP(题名2, 16) AS 题名,DUMP(责任者2, 16) AS 责任者,DUMP(出版者2, 16) AS 出版者, 出版日期, 版次,标准编码,处理日期,索书号,库键码 From 馆藏书目库 WHERE ROWNUM<" + jgs + " AND " + sql;
                    }
                    else
                    {
                        if (this.acCheck.Checked && this.acCheck.Enabled)
                            sql = "SELECT TOP " + jgs + " "+fileds+", d.登录号,d.条形码,g.索书号, g.标准编码,g.题名, g.责任者,g.出版者, g.出版日期, g.版次,g.主键码 From 馆藏典藏库 d left join 馆藏书目库 g on d.主键码 = g.主键码 WHERE " + sql;
                        else
                            sql = "SELECT TOP " + jgs + " " + fileds + " From 馆藏书目库 g WHERE " + sql;
                    }
                    if (!this.bxCheck.Checked)
                        sql = sql + " AND g.库键码=" + this._kjm;
                    if (this.ztKay.Text.Equals("入藏"))
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE g.主键码 = 馆藏典藏库.主键码 and 状态= 'G')";
                    if (this.ztKay.Text.Equals("验收"))
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE g.主键码 = 采购库.主键码 and 状态= 'C' and 登录号 is not null )";
                    if (this.ztKay.Text.Equals("采购"))
                        sql = sql + " AND EXISTS (SELECT 主键码 FROM 采购库 WHERE g.主键码 = 采购库.主键码 and 状态= 'C' and 登录号 is null)";
                    if (this.ztKay.Text.Equals("其他"))
                    {
                        sql = sql + " AND (EXISTS (SELECT 主键码 FROM 馆藏典藏库 WHERE g.主键码 = 馆藏典藏库.主键码 and 状态= 'L') ";
                        sql = sql + " OR EXISTS (SELECT 主键码 FROM 采购库 WHERE g.主键码 = 采购库.主键码 and 状态= 'L')) ";
                    }
                    string stime = string.Empty, etime = string.Empty;
                    if (this.rcCheck.Checked)
                    {
                        stime = this.sTime.Text;
                        etime = this.eTime.Text;
                        sql += "AND EXISTS (SELECT 主键码 FROM 采购库 WHERE g.主键码 = 采购库.主键码 and (入藏日期 BETWEEN '" + stime + "' AND '" + etime + "'))";
                    }
                    if (this.ydrqCheck.Checked)
                    {
                        string dstime = this.dsTime.Text;
                        string detime = this.deTime.Text;
                        sql += "AND EXISTS (SELECT 主键码 FROM 采购库 WHERE g.主键码 = 采购库.主键码 and (预订日期 BETWEEN '" + dstime + "' AND '" + detime + "'))";
                    }
                    if (this.ysrqCheck.Checked)
                    {
                        ystime = this.ysTime.Text;
                        yetime = this.yeTime.Text;
                        sql += "AND EXISTS (SELECT 主键码 FROM 采购库 WHERE g.主键码 = 采购库.主键码 and (验收日期 BETWEEN '" + ystime + "' AND '" + yetime + "'))";
                    }
                    else
                    {
                        ystime = "";
                        yetime = "";
                    }
                    if (this.pfsKay.SelectedIndex == 1)
                        sql += " order by " + this.pzdKay.Text + " DESC";
                    else
                        sql += " order by " + this.pzdKay.Text;
                    this.Sql = sql;

                    if (this.BtnScarchClick != null)
                    {
                        this.BtnScarchClick(sender, e);
                    }
                }
            }
        }
    }
}
